//
//  LBWDiscoverSearchView.swift
//  weibo
//
//  Created by Limonwei on 15/12/5.
//  Copyright © 2015年 heima. All rights reserved.
//

import UIKit

class LBWDiscoverSearchView: UIView {

    @IBOutlet weak var cancelButton: UIButton!
    @IBOutlet weak var textFieldRightCons: NSLayoutConstraint!
    @IBOutlet weak var textField: LBWTextField!
    
    
    @IBAction func cancelButtonClick(sender: UIButton) {
        
        //取消第一响应者
        textField.resignFirstResponder()
        //self.endEditing(true)
        
        //执行动画
        self.textFieldRightCons.constant = 0
        
        UIView.animateWithDuration(0.25) { () -> Void in
            //
            self.textField.layoutIfNeeded()
        }
        
    }
    
    func textFieldDidBeginEditing(textField: UITextField){
        self.textFieldRightCons.constant = self.cancelButton.frame.width
        
        UIView.animateWithDuration(0.25) { () -> Void in
            //
            self.textField.layoutIfNeeded()
        }
        
    }
    
    
    //从xib里面加载View
    class func searchView() -> LBWDiscoverSearchView{
        
        return NSBundle.mainBundle().loadNibNamed("LBWDiscoverSearchView", owner: nil, options: nil).last! as! LBWDiscoverSearchView
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    override func awakeFromNib() {
        //设置放大镜图片
        let leftView = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        leftView.frame = CGRectMake(0, 0, frame.height, frame.height)
        
        //显示模式
        leftView.contentMode = .Center
        
        textField.leftView = leftView
        
        textField.leftViewMode = UITextFieldViewMode.Always
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}













